/**
 * 
 */
package com.greatwideweb.runhelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;



import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * @author dave
 *
 */
public class DataArrayAdapter extends ArrayAdapter<RunItem> 
{

  private Activity context;
  private TextView txtDistance;
  private TextView txtDate;
  private ArrayList<RunItem> items;
  private final String EMPTY_SPACE =" ";
  private final String SEPERATOR =":";
  
  
  public DataArrayAdapter(Activity context, int textViewResourceId, ArrayList<RunItem> items) 
  {
    super(context, textViewResourceId, 0, items);
    this.context = context;
    this.items = items;
    // TODO Auto-generated constructor stub
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) 
  {
    RunItem item = this.items.get(position);
    View row = convertView;
    if(row==null)
    {
      LayoutInflater inflater = context.getLayoutInflater();
      row = inflater.inflate( R.layout.data_item, parent, false);
    }
    
    if(position == 0)
      row.setBackgroundColor(0x00FFFF00 );
    else if(position == 1)
        row.setBackgroundColor(0xFF00FF00 );
    
    txtDistance = (TextView) row.findViewById(R.id.txtDistance);
    txtDistance.setText(getDistance(item));
    
    txtDate = (TextView) row.findViewById(R.id.txtDate);
    String dateString = getDate(item);
    txtDate.setText("" + dateString);
    
    return row;
  }
  
  public RunItem getSelectedItem(int position)
  {
    return this.items.get(position);
  }
  
  public String getDistance(RunItem item)
  {
    return String.valueOf(item.getDistance())  + EMPTY_SPACE + SEPERATOR  + EMPTY_SPACE + 
        String.valueOf(item.getDistancePart()) ;
  }
  
  public String getDate(RunItem item)
  {
    GregorianCalendar c = item.getRunDate();
    String result = getDayofWeekDescr(c) + EMPTY_SPACE + 
                    getMonthDescr(c) + EMPTY_SPACE + 
                    c.get(Calendar.DATE) + EMPTY_SPACE + c.get(Calendar.YEAR);
    
    return result;
  }

  private String getMonthDescr(GregorianCalendar c)
  {
    String result=null;
    int i = c.get(Calendar.MONTH);

   switch(i)
   {
     case 0:
     {
       result = "January";
       break;
     }
     
     case 1:
     {
       result = "February";
       break;
     }
     
     case 2:
     {
       result = "March";
       break;
     }
     
     case 3:
     {
       result = "April";
       break;
     }
     
     case 4:
     {
       result = "May";
       break;
     }
     
     case 5:
     {
       result = "June";
       break;
     }
     
     case 6:
     {
       result = "July";
       break;
     }
     
     case 7:
     {
       result = "August";
       break;
     }
     
     case 8:
     {
       result = "September";
       break;
     }
     
     case 9:
     {
       result = "October";
       break;
     }
     
     case 10:
     {
       result = "November";
       break;
     }
     
     case 11:
     {
       result = "December";
       break;
     }
   }
    return result;
  }

  private String getDayofWeekDescr(GregorianCalendar c)
  {
    String result=null;
    int i = c.get(Calendar.DAY_OF_WEEK);

    if(i == 2)
    {
      result = "Monday";           
    } else if (i==3)
    {
      result = "Tuesday";
    } else if (i==4)
    {
      result = "Wednesday";
    } else if (i==5)
    {
      result = "Thursday";
    } else if (i==6)
    {
      result = "Friday";
    } else if (i==7)
    {
      result = "Saturday";
    } else if (i==1)
    {
      result = "Sunday";
    }
    return result;
  }

}
